home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / LAZY.PY < prev    next >
Encoding:
Text File  |  2000-05-11  |  8.7 KB  |  283 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 0.9.7
  3. # ---------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__='''$Id: Lazy.py,v 1.2 2000/05/11 18:54:16 jim Exp $'''
  65. __version__='$Revision: 1.2 $'[11:-2]
  66.  
  67.  
  68. class Lazy:
  69.  
  70.     # Allow (reluctantly) access to unprotected attributes
  71.     __allow_access_to_unprotected_subobjects__=1
  72.  
  73.     def __repr__(self): return `list(self)`
  74.     
  75.     def __len__(self):
  76.  
  77.         try: return self._len
  78.         except AttributeError: pass
  79.  
  80.         l=len(self._data)
  81.         while 1:
  82.             try:
  83.                 self[l]
  84.                 l=l+1
  85.             except:
  86.                 self._len=l
  87.                 return l
  88.  
  89.     def __getslice__(self,i1,i2):
  90.         r=[]
  91.         for i in range(i1,i2):
  92.             try: r.append(self[i])
  93.             except IndexError: return r
  94.         return r
  95.  
  96.     slice=__getslice__
  97.  
  98. class LazyCat(Lazy):
  99.     # Lazy concatenation of one or more sequences.  Should be handy
  100.     # for accessing small parts of big searches.
  101.     
  102.     def __init__(self, sequences):
  103.         self._seq=sequences
  104.         self._data=[]
  105.         self._sindex=0
  106.         self._eindex=-1
  107.  
  108.     def __getitem__(self,index):
  109.  
  110.         data=self._data
  111.         try: seq=self._seq
  112.         except AttributeError: return data[index]
  113.  
  114.         i=index
  115.         if i < 0: i=len(self)+i
  116.         if i < 0: raise IndexError, index
  117.  
  118.         ind=len(data)
  119.         if i < ind: return data[i]
  120.         ind=ind-1
  121.  
  122.         sindex=self._sindex
  123.         try: s=seq[sindex]
  124.         except: raise IndexError, index
  125.         eindex=self._eindex
  126.         while i > ind:
  127.             try:
  128.                 eindex=eindex+1
  129.                 v=s[eindex]
  130.                 data.append(v)
  131.                 ind=ind+1
  132.             except IndexError:
  133.                 self._sindex=sindex=sindex+1
  134.                 try: s=self._seq[sindex]
  135.                 except:
  136.                     del self._seq
  137.                     del self._sindex
  138.                     del self._eindex
  139.                     raise IndexError, index
  140.                 self._eindex=eindex=-1
  141.         self._eindex=eindex
  142.         return data[i]
  143.  
  144. class LazyMap(Lazy):
  145.     # Act like a sequence, but get data from a filtering process.
  146.     # Don't access data until necessary
  147.  
  148.     def __init__(self,func,seq):
  149.         self._seq=seq
  150.         self._len=len(seq)
  151.         self._data=[]
  152.         self._func=func
  153.  
  154.     def __getitem__(self,index):
  155.  
  156.         data=self._data
  157.         try: s=self._seq
  158.         except AttributeError: return data[index]
  159.  
  160.         i=index
  161.         if i < 0: i=len(self)+i
  162.         if i < 0: raise IndexError, index
  163.  
  164.         ind=len(data)
  165.         if i < ind: return data[i]
  166.         ind=ind-1
  167.  
  168.         func=self._func
  169.         while i > ind:
  170.             try:
  171.                 ind=ind+1
  172.                 data.append(func(s[ind]))
  173.             except IndexError:
  174.                 del self._func
  175.                 del self._seq
  176.                 raise IndexError, index
  177.         return data[i]
  178.  
  179. class LazyFilter(Lazy):
  180.     # Act like a sequence, but get data from a filtering process.
  181.     # Don't access data until necessary
  182.  
  183.     def __init__(self,test,seq):
  184.         self._seq=seq
  185.         self._data=[]
  186.         self._eindex=-1
  187.         self._test=test
  188.  
  189.     def __getitem__(self,index):
  190.  
  191.         data=self._data
  192.         try: s=self._seq
  193.         except AttributeError: return data[index]
  194.  
  195.         i=index
  196.         if i < 0: i=len(self)+i
  197.         if i < 0: raise IndexError, index
  198.  
  199.         ind=len(data)
  200.         if i < ind: return data[i]
  201.         ind=ind-1
  202.  
  203.         test=self._test
  204.         e=self._eindex
  205.         while i > ind:
  206.             try:
  207.                 e=e+1
  208.                 v=s[e]
  209.                 if test(v):
  210.                     data.append(v)
  211.                     ind=ind+1
  212.             except IndexError:
  213.                 del self._test
  214.                 del self._seq
  215.                 del self._eindex
  216.                 raise IndexError, index
  217.         self._eindex=e
  218.         return data[i]
  219.  
  220. class LazyMop(Lazy):
  221.     # Act like a sequence, but get data from a filtering process.
  222.     # Don't access data until necessary
  223.  
  224.     def __init__(self,test,seq):
  225.         self._seq=seq
  226.         self._data=[]
  227.         self._eindex=-1
  228.         self._test=test
  229.  
  230.     def __getitem__(self,index):
  231.  
  232.         data=self._data
  233.         try: s=self._seq
  234.         except AttributeError: return data[index]
  235.  
  236.         i=index
  237.         if i < 0: i=len(self)+i
  238.         if i < 0: raise IndexError, index
  239.  
  240.         ind=len(data)
  241.         if i < ind: return data[i]
  242.         ind=ind-1
  243.  
  244.         test=self._test
  245.         e=self._eindex
  246.         while i > ind:
  247.             try:
  248.                 e=e+1
  249.                 v=s[e]
  250.                 try:
  251.                     v=test(v)
  252.                     data.append(v)
  253.                     ind=ind+1
  254.                 except: pass
  255.             except IndexError:
  256.                 del self._test
  257.                 del self._seq
  258.                 del self._eindex
  259.                 raise IndexError, index
  260.         self._eindex=e
  261.         return data[i]
  262.